/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.Controleur;
import Modele.Grille;
import static com.sun.java.accessibility.util.AWTEventMonitor.addKeyListener;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author Raphaël
 */
public class Fenetre extends JFrame implements Observer {

    JComponent cadrillage = new JPanel(new GridLayout(20, 10));
    JComponent menu = new JPanel(new BorderLayout());
    JComponent apercu = new JPanel(new GridLayout(4, 4));
    JComponent score = new JTextArea();
    Controleur controle;

    public Fenetre(final Controleur controle) {
        int height = 200;
        int width = 200;
        int height2 = 200;
        int width2 = 100;

        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {
                super.windowClosing(arg0);
                System.exit(0);
            }
        });

        setFocusable(true);
        addKeyListener(new TAdapter());
        JMenuBar jm = new JMenuBar();
        JMenu m = new JMenu("Jeu");
        //JButton bouton = new JButton("START");
        JMenuItem nouvellePartie = new JMenuItem("Nouvelle Partie");
        nouvellePartie.setMnemonic(KeyEvent.VK_N);
        nouvellePartie.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                nouvellePartie();
            }
        });
        JMenuItem pause = new JMenuItem("Pause");
        pause.setMnemonic(KeyEvent.VK_P);
        pause.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                pause();
            }
        });
        this.controle = controle;
        this.cadrillage = new JPanel(new GridLayout(20, 10));
        this.cadrillage.setPreferredSize(new Dimension(width, height));
        this.cadrillage.setBackground(Color.lightGray);
        this.menu = new JPanel(new FlowLayout());
        this.menu.setPreferredSize(new Dimension(width2, height2));
        this.apercu = new JPanel(new GridLayout(4, 4));
        this.apercu.setPreferredSize(new Dimension(75, 75));

        setTitle("Tetris D&S");
        setSize(width + width2 + 6, 400);
        setLocationRelativeTo(null); //On centre la fenêtre sur l'écran
        setResizable(false); //On interdit la redimensionnement de la fenêtre
        JComponent pan = new JPanel(new BorderLayout());

        m.add(nouvellePartie);
        m.add(pause);
        jm.add(m);
        setJMenuBar(jm);
        pan.add("West", cadrillage);
        pan.add("East", menu);
        menu.add(apercu);
        menu.add(score);
        add(pan);
        afficherGrille(controle.tab.grille);
        afficherApercu(controle.tab.grille);
        /*menu.add(bouton);
         bouton.addActionListener(new ActionListener() { 
         public void actionPerformed(ActionEvent e) { 
         if(((JButton)e.getSource()).getText()=="START"){
         nouvellePartie();
         controle.tab.nouvellePiece();
         controle.tab.demarrerTimer();                 
         ((JButton)e.getSource()).setText("PAUSE");
         }
         else{
         if(((JButton)e.getSource()).getText()=="PAUSE"){
         pause();
         ((JButton)e.getSource()).setText("PLAY");
         }
         else{
         if(((JButton)e.getSource()).getText()=="PLAY"){
         pause(); 
         ((JButton)e.getSource()).setText("PAUSE");
         }
         }
         }
               
         } 
         }); 
         //this.controle.tab.ajoutObservateur(this);*/
    }

    public void afficherApercu(int[][] tab) {
        int i, j;
        this.apercu.removeAll();
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; ++j) {
                JComponent cellule;
                cellule = new Case((int) tab[j][i], j + 3, i - 4);
                this.controle.tab.ajoutObservateur((Observer) cellule);
                this.apercu.add(cellule);
            }
        }
    }

    public void afficherGrille(int[][] tab) {
        int i, j;
        this.cadrillage.removeAll();
        for (i = 0; i < 20; i++) {
            for (j = 0; j < 10; ++j) {
                JComponent cellule;
                cellule = new Case((int) tab[j][i], j, i);
                this.controle.tab.ajoutObservateur((Observer) cellule);
                this.cadrillage.add(cellule);
            }
        }
    }

    public void nouvellePartie() {
        this.controle.partie();
    }

    public void pause() {
        this.controle.pause();
    }

    @Override
    public void update(Observable obs, Object arg) {
        if (obs instanceof Grille) {
        }

    }

    class TAdapter extends KeyAdapter {

        @Override
        public void keyPressed(KeyEvent e) {
            int keycode = e.getKeyCode();
            controle.mouvement(keycode);
        }

    }
}
